import json
from copy import deepcopy
import numpy as np

if __name__ == "__main__":
    voltages = np.linspace(-80, -50, 10)
    with open("config/features_prots.json", "r") as f:
        features = json.load(f)
    prot_base = features["protocols"][0]

    for feat in features["efeatures"]:
        if (
            feat["efel_feature_name"] == "all_burst_number"
            and feat["protocol_name"] == "Step_ReboundBurst_burst"
        ):
            feat_base = feat

    for voltage in voltages:
        prot = deepcopy(prot_base)
        prot["stimuli"][0]["holding_voltage"] = voltage
        prot["name"] = f"Step_ReboundBurst_burst_{voltage}"
        prot["recordings_from_config"] = [
            {
                "type": "CompRecording",
                "name": f"Step_ReboundBurst_burst_{voltage}.soma.v",
                "location": "soma",
                "variable": "v",
            }
        ]
        print(prot)
        features["protocols"].append(prot)

        feat = deepcopy(feat_base)
        feat["protocol_name"] = prot["name"]
        features["efeatures"].append(feat)
        features["efeatures"].append(
            {
                "efel_feature_name": "voltage_base",
                "protocol_name": prot["name"],
                "recording_name": "soma.v",
                "mean": -20.0,
                "original_std": 0.0,
                "efeature_name": "voltage_base",
                "efel_settings": {
                    "strict_stiminterval": True,
                    "Threshold": -50.0,
                    "interp_step": 0.025,
                    "stim_start": 2000.0,
                    "stim_end": 5000.0,
                    "ignore_first_spike": 0,
                },
            }
        )

    print(prot_base)

    with open("config/features_prots.json", "w") as f:
        json.dump(features, f, indent=4)
